IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_UserAssignGroups]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_UserAssignGroups]
GO
create procedure [dbo].[cms_sp_UserAssignGroups]
@UserId int,
@UserGroupIds varchar(8000)
as
declare @userGroupLinks table
(
UserId int,
UserGroupId int
)

merge into dbo.UserGroupLinks
using (select 	
			tmp.integer UserGroupId,
			@UserId UserId
		from dbo.cms_udf_SplitIntegers(@UserGroupIds) as tmp) as NewUserGroups
	on UserGroupLinks.UserGroupId = NewUserGroups.UserGroupId 
	  and UserGroupLinks.UserId = NewUserGroups.UserId 
when not matched then
	insert (UserGroupId, UserId)
	values (NewUserGroups.UserGroupId, NewUserGroups.UserId);
		
GO

